package com.iscas.green.extralindextest.controller;

import com.iscas.green.extralindextest.service.DataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api")
public class DataController {

    private static final Logger log = LoggerFactory.getLogger(DataController.class);
    
    private final DataService dataService;

    @Autowired
    public DataController(DataService dataService) {
        this.dataService = dataService;
    }

    @GetMapping("/mysql/test")
    public String testMysql() {
        dataService.testMysql();
        return "MySQL 测试完成，请查看日志";
    }

    @GetMapping("/postgresql/test")
    public ResponseEntity<String> testPostgresql() {
        try {
            dataService.testPostgresql();
            return ResponseEntity.ok("PostgreSQL 测试完成，请查看日志");
        } catch (Exception e) {
            log.error("PostgreSQL测试失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("PostgreSQL 测试失败: " + e.getMessage() + "\n请查看详细日志");
        }
    }

    @PostMapping("/postgresql/insert")
    public ResponseEntity<String> insertPostgresqlData(@RequestBody String data) {
        return dataService.insertPostgresqlData(data);
    }

    @GetMapping("/clickhouse/test")
    public String testClickhouse() {
        dataService.testClickhouse();
        return "ClickHouse 测试完成，请查看日志";
    }

    @GetMapping("/clickhouse/data")
    public Map<String, Object> getClickhouseData() {
        return dataService.getClickhouseData();
    }

    @GetMapping("/mongodb/test")
    public String testMongoDB() {
        dataService.testMongoDB();
        return "MongoDB 测试完成，请查看日志";
    }

    @GetMapping("/mongodb/data")
    public ResponseEntity<?> getMongoData() {
        return dataService.getMongoData();
    }

    @PostMapping("/redis/lettuce")
    public ResponseEntity<String> testLettuce(@RequestBody String value) {
        return dataService.testLettuce(value);
    }
} 